/* 
 * The MIT License
 *
 * Copyright 2014 Venkat Ram Akkineni.
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 */
package org.netbeans.modules.tfsnb.tfvc.options;

import java.beans.PropertyChangeListener;
import java.beans.PropertyChangeSupport;
import javax.swing.JComponent;
import org.netbeans.spi.options.OptionsPanelController;
import org.openide.util.HelpCtx;
import org.openide.util.Lookup;

/**
 *
 * @author Venkat Ram Akkineni
 */
public class TFSOptionsPanelController extends OptionsPanelController {
    
    public TFSOptionsPanel tfsOptionsPanel;
    private boolean changed;
    private final PropertyChangeSupport pcs = new PropertyChangeSupport(this);

    public TFSOptionsPanelController() {
        tfsOptionsPanel = new TFSOptionsPanel(this);        
    }

    @Override
    public void update() {
        getTfsOptionsPanel().load();
        changed = false;
    }

    @Override
    public void applyChanges() {
        if (!validateFields()) return;
        
        getTfsOptionsPanel().store();
        changed = false;
    }

    @Override
    public void cancel() {
        // need not do anything special, if no changes have been persisted yet
    }

    @Override
    public boolean isValid() {
        return getTfsOptionsPanel().valid();
    }

    @Override
    public boolean isChanged() {
        return changed;
    }

    @Override
    public JComponent getComponent(Lookup lkp) {
        return getTfsOptionsPanel();
    }

    @Override
    public HelpCtx getHelpCtx() {
        return new HelpCtx("org.netbeans.modules.tfsnb.tfvc.options.TFSOptionsPanelController");
    }

    @Override
    public void addPropertyChangeListener(PropertyChangeListener pl) {
        pcs.addPropertyChangeListener(pl);
    }

    @Override
    public void removePropertyChangeListener(PropertyChangeListener pl) {
        pcs.removePropertyChangeListener(pl);
    }

    public TFSOptionsPanel getTfsOptionsPanel() {
        if (tfsOptionsPanel == null) {
            tfsOptionsPanel = new TFSOptionsPanel(this);
        }
        return tfsOptionsPanel;
    }

    public void setTfsOptionsPanel(TFSOptionsPanel tfsOptionsPanel) {
        this.tfsOptionsPanel = tfsOptionsPanel;
    }    

    /**
     * Form validation stuff.
     * @return 
     */
    private boolean validateFields() {
        return true;
    }  
    
    
}
